package com.shadt.pay.controller;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shadt.core.controller.BaseController;
import com.shadt.core.util.IpUtil;
import com.shadt.core.util.XMLUtil;
import com.shadt.pay.entity.Pay_Shadt_Order;
import com.shadt.pay.service.PrePayService;
import com.shadt.pay.wechat.service.WeChatPayService;

/**
 * 预支付控制器
 * 
 * @author SongR
 * 
 */
@Controller
@RequestMapping("/prepay")
public class PrePayController extends BaseController {
	@Autowired
	PrePayService prePayService;
	
	@Autowired
	WeChatPayService wechatpayService;
	
	/**
	 * 统一下单请求
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/unifiedorder")
	public void unifiedOrder(HttpServletRequest request,HttpServletResponse response,ServletInputStream inputStream) {
		try {
			String s = XMLUtil.inputStreamToString(inputStream);
			String respXml = prePayService.unifiedOrder(s,IpUtil.getIpAddr(request));
			response.getWriter().print(respXml);
		} catch (Exception e) {
			try {
				response.getWriter().print("<xml><return_code>FAIL</return_code><return_msg>系统异常！</return_msg></xml>");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	
	/**
	 * 统一支付请求
	 * 返回支付方式页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/pay")
	public String pay(HttpServletRequest request, HttpServletResponse response) {
		try {
			String s ="<xml><appid>"+request.getParameter("appid")+"</appid><mch_id>"+request.getParameter("mch_id")+"</mch_id>" +
					"<out_trade_no>"+request.getParameter("out_trade_no")+"</out_trade_no><redirect_uri>"+request.getParameter("redirect_uri")+"</redirect_uri>" +
					"</xml>";
			System.out.println("支付方式页面请求参数："+s);
			Pay_Shadt_Order order = prePayService.pay(s);
			if(order==null){
				System.out.println(null+"null");
			}else{
				request.setAttribute("payinfo", order);
				
				if(order.getOrder_trade_status()==0||order.getOrder_trade_status()==1){
					return "phone/pay";
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return "phone/msg";
	}

	/**
	 * shadt支付结果通知
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/notify")
	public void notify(HttpServletRequest request, HttpServletResponse response,ServletInputStream inputStream){
		try {
			String reqXml = XMLUtil.inputStreamToString(inputStream);
			wechatpayService.notity(reqXml);
			response.getWriter().print("<xml><return_code>SUCCESS</return_code></xml>");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	   
	/**
	 * shadt查询订单
	 */
	@RequestMapping("/orderquery")
	public void orderQuery(HttpServletRequest request, HttpServletResponse response,ServletInputStream inputStream){
		try {
			String reqXml = XMLUtil.inputStreamToString(inputStream);
			String s = prePayService.orderQuery(reqXml);
			response.getWriter().print(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
